<?php

class IndexController extends Zend_Controller_Action
{
    function init()
    {
        $this->db = Zend_Registry::get("db");
    }
    
    function preDispatch()
    {
		$params = $this->_request->getParams();
		
		if(isset($params['testmode']) && 1==$params['testmode'])
		{
			$_SESSION['identity']['testmode'] = 1;
		}
		
		if(!$_SESSION['identity']['testmode'])
		{
			//weixin share
			$jssdk = new Sys_Jssdk();
			$this->view->signPackage = $jssdk->GetSignPackage();
			
			$mod_ext = new Sys_Extensions();
			$this->view->wechat_share_title = "我的精品库";
			$this->view->wechat_share_link = $mod_ext->WechatUrlDecolation('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
			$this->view->wechat_share_imgUrl = "http://cms.bianjiemao.com/images/bjm.jpg";
			$this->view->wechat_share_desc = "淘宝天猫打折最厉害的精品都在我这里!";
		}
    }
	
    function indexAction()
    {
        $params = $this->_request->getParams();
		
		$this->view->nick_name = "我";
		
		$mod_parmas = new Db_Tables_Params();
		$this->view->front_end_domain = $mod_parmas->GetVal("PRODUCT_PIC_DOMAIN");
		$this->view->back_end_domain = $mod_parmas->GetVal("PRODUCT_PIC_DOMAIN_ADMIN");
		
		$mod_cat = new Db_Tables_BjmCategories();
		$this->view->cat = $mod_cat->GetCategories();
		
		if($params['i'])
		{
			$member_id = intval($params['i']);
		}
		
		if($params['o'])
		{
			$od = intval($params['o']);
			
			if(!in_array($od, array(1,2)))
			{
				$od = 1;
			}
		}else{
			$od = 1;
		}
		
		if($member_id)
		{
			$mod_members = new Db_Tables_BjmMembers();
			$member_row = $mod_members->fetchRow("member_id = ".$member_id);
			
			if($member_row && $member_row->nick_name)
			{
				$this->view->nick_name = $member_row->nick_name;
			}
			
			$mod_tables = new Db_Joins_TablesJoins();
			$mod_tables->member_id = $member_id;

			$params_pool = array("i=".$member_id, "o=".$od);
			
			$this->view->i = $member_id;
			$this->view->od = $od;
			
			if($params['keyword'])
			{
				$mod_tables->keyword = $params['keyword'];
				$this->view->keyword = $params['keyword'];
				$params_pool[] = "keyword=".$params['keyword'];
			}
			
			if($params['cid'])
			{
				$mod_tables->cid = $params['cid'];
				$this->view->cid = $params['cid'];
				$params_pool[] = "cid=".$params['cid'];
			}

			if(!$params['page_id'])
			{
				$page_id = 1;
			}else{
				$page_id = $params['page_id'];
			}

			if($page_id)
			{
				$mod_tables->page_id = $page_id;
				$this->view->page_id = $page_id;
			}

			$result = $mod_tables->GetProducts();
			$this->view->data = $result['current_page_info'];
			
			//make pagination
			if(!empty($params_pool))
			{
				$params_string = implode("&", $params_pool);
			}else{
				$params_string = "";
			}

			$this->view->params_string = $params_string;
			$this->view->total_pages = $result['total_pages'];
		}
    }
	
    function detailsAction()
    {
        $params = $this->_request->getParams();
		
		if(intval($params['i']) && intval($params['product_id']))
		{
			$mod_tables_joins = new Db_Joins_TablesJoins();
			$mod_tables_joins->member_id = intval($params['i']);
			$mod_tables_joins->product_id = intval($params['product_id']);
			$this->view->data = $mod_tables_joins->GetProductDetails();
			$this->view->member_id = intval($params['i']);
			
			$mod_params = new Db_Tables_Params();
			if($this->view->data['upload_by'])
			{
				$site = $mod_params->GetVal("PRODUCT_PIC_DOMAIN");
			}else{
				$site = $mod_params->GetVal("PRODUCT_PIC_DOMAIN_ADMIN");
			}
			
			$this->view->wechat_share_title = trim($this->view->data['short_title']);
			$this->view->wechat_share_imgUrl = trim($site."/".$this->view->data['pic_local']);
			$this->view->wechat_share_desc = trim($this->view->data['short_desc']);
		}else{
			die("Invalid Action.");
		}
	}
}